package volcengine

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
)

type DictResquest struct {
	Language string `json:"language"`
	Text     string `json:"text"`
}

type DictResponse struct {
	Words []struct {
		Source  int    `json:"source"`
		Text    string `json:"text"`
		PosList []struct {
			Type      int `json:"type"`
			Phonetics []struct {
				Type int    `json:"type"`
				Text string `json:"text"`
			} `json:"phonetics"`
			Explanations []struct {
				Text     string `json:"text"`
				Examples []struct {
					Type      int `json:"type"`
					Sentences []struct {
						Text      string `json:"text"`
						TransText string `json:"trans_text"`
					} `json:"sentences"`
				} `json:"examples"`
				Synonyms []interface{} `json:"synonyms"`
			} `json:"explanations"`
			Relevancys []interface{} `json:"relevancys"`
		} `json:"pos_list"`
	} `json:"words"`
	Phrases  []interface{} `json:"phrases"`
	BaseResp struct {
		StatusCode    int    `json:"status_code"`
		StatusMessage string `json:"status_message"`
	} `json:"base_resp"`
}

func Query(queryWord string) {
	client := &http.Client{}
	request := DictResquest{Text: queryWord, Language: "en"}
	buf, err := json.Marshal(request)
	if err != nil {
		log.Fatal(err)
	}
	var data = bytes.NewReader(buf)
	req, err := http.NewRequest("POST", "https://translate.volcengine.com/web/dict/match/v1/?msToken=&X-Bogus=DFSzswVLQDc7EiQrSWOwhKXAIQ-z&_signature=_02B4Z6wo00001G5yNMQAAIDBDXj0r1F3FuhucjBAAHnq65SNGmQvrjg9h9dB1sm.wQMFEDHqy1jGXupUU1s5mIWCZ0hM3Zx9vi1PpMKWfPXlXV8ZwbfhiOLfyEjG7gu80q4b14rshro4kPfRd9", data)
	if err != nil {
		log.Fatal(err)
	}
	req.Header.Set("authority", "translate.volcengine.com")
	req.Header.Set("accept", "application/json, text/plain, */*")
	req.Header.Set("accept-language", "zh-CN,zh;q=0.9")
	req.Header.Set("content-type", "application/json")
	req.Header.Set("cookie", "x-jupiter-uuid=16524467489791958; i18next=zh-CN; s_v_web_id=verify_520cdfb9fe88041f23346d60bd5ae8ed; _tea_utm_cache_2018=undefined; ttcid=b558367f6df942cdb7e9395a76d8226f11; tt_scid=FHRg4V8.3uNvj9Uxnb3ybQ1Svak-ng0CrABDyyN-oLexvwZoSHGjP1QhQg13qSLC7328")
	req.Header.Set("origin", "https://translate.volcengine.com")
	req.Header.Set("referer", "https://translate.volcengine.com/translate?category=&home_language=zh&source_language=en&target_language=zh&text=good")
	req.Header.Set("sec-ch-ua", `" Not A;Brand";v="99", "Chromium";v="101", "Google Chrome";v="101"`)
	req.Header.Set("sec-ch-ua-mobile", "?0")
	req.Header.Set("sec-ch-ua-platform", `"Windows"`)
	req.Header.Set("sec-fetch-dest", "empty")
	req.Header.Set("sec-fetch-mode", "cors")
	req.Header.Set("sec-fetch-site", "same-origin")
	req.Header.Set("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36")
	resp, err := client.Do(req)
	if err != nil {
		log.Fatal(err)
	}
	defer resp.Body.Close()
	bodyText, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Fatal(err)
	}
	if resp.StatusCode != 200 {
		log.Fatal("bad StatusCode:", resp.StatusCode, "body", string(bodyText))
	}
	var dictResponse DictResponse
	err = json.Unmarshal(bodyText, &dictResponse)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println("-------------------")
	fmt.Println("   火山翻译查询结果")
	fmt.Println("-------------------")
	for _, word := range dictResponse.Words {
		fmt.Println(queryWord, "UK: [", word.PosList[0].Phonetics[0].Text, "] US: [", word.PosList[0].Phonetics[1].Text, "]")
		for _, item := range word.PosList {
			for _, explanation := range item.Explanations {
				fmt.Println(explanation.Text)
				for _, example := range explanation.Examples {
					for _, sentence := range example.Sentences {
						fmt.Println(sentence.Text, sentence.TransText)
					}
				}
			}
		}
	}
}
